﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jalal.Library.Data;
using System.Data.Common;
using System.Data;
using Jalal.Library.Utility;

namespace BCGC.DAL
{
    public class EmailTemplateData
    {
        public static DataTable GetTemplateByTitle(string templateTitle)
        {
            DataMiner dataMiner = DataFactory.GetDataMiner();
            DbCommand command = DataFactory.GetCommand();
            command.CommandText = "CSP_GetMailTemplateByTitle";
            dataMiner.AddParameter(command, "@TemplateTitle", DbType.String, 500, ParameterDirection.Input, templateTitle);
            return dataMiner.GetListData(command);
        }

        public static DataTable GetTemplateById(int templateId)
        {
            DataMiner dataMiner = DataFactory.GetDataMiner();
            DbCommand command = DataFactory.GetCommand();
            command.CommandText = "CSP_GetMailTemplateById";
            dataMiner.AddParameter(command, "@Id", DbType.Int32, 0, ParameterDirection.Input, templateId);
            return dataMiner.GetListData(command);
        }

        public static DataTable GetTemplateList()
        {
            object id = null;
            DataMiner dataMiner = DataFactory.GetDataMiner();
            DbCommand command = DataFactory.GetCommand();
            command.CommandText = "CSP_GetMailTemplateList";
            return dataMiner.GetListData(command);
        }

        public static int CreateNewEmailTemplate(string title, string subject, string body)
        {
            object id = null;
            DataMiner dataMiner = DataFactory.GetDataMiner();
            DbCommand command = DataFactory.GetCommand();
            command.CommandText = "CSP_CreateNewEmailTemplate";
            dataMiner.AddParameter(command, "@Title", DbType.String, -1, ParameterDirection.Input, title);
            dataMiner.AddParameter(command, "@Subject", DbType.String, -1, ParameterDirection.Input, subject);
            dataMiner.AddParameter(command, "@Body", DbType.String, -1, ParameterDirection.Input, body);
            dataMiner.AddParameter(command, "ReturnValue", DbType.Int32, 0, ParameterDirection.ReturnValue, null);
            dataMiner.Execute(command, out id);

            return (int)CommonUtility.FilterNull(id, typeof(int));
        }

        public static int UpdateEmailTemplate(int templateId, string title, string subject, string body)
        {
            object count = null;
            DataMiner dataMiner = DataFactory.GetDataMiner();
            DbCommand command = DataFactory.GetCommand();
            command.CommandText = "CSP_UpdateEmailTemplate";
            dataMiner.AddParameter(command, "@Id", DbType.Int32, 0, ParameterDirection.Input, templateId);
            dataMiner.AddParameter(command, "@Title", DbType.String, -1, ParameterDirection.Input, title);
            dataMiner.AddParameter(command, "@Subject", DbType.String, -1, ParameterDirection.Input, subject);
            dataMiner.AddParameter(command, "@Body", DbType.String, -1, ParameterDirection.Input, body);
            dataMiner.AddParameter(command, "ReturnValue", DbType.Int32, 0, ParameterDirection.ReturnValue, null);
            dataMiner.Execute(command, out count);

            return (int)CommonUtility.FilterNull(count, typeof(int));
        }

        public static int CountTemplateByTitle(string title)
        {
            object count = null;
            DataMiner dataMiner = DataFactory.GetDataMiner();
            DbCommand command = DataFactory.GetCommand();
            command.CommandText = "CSP_CountTemplateByTitle";
            dataMiner.AddParameter(command, "@Title", DbType.String, 500, ParameterDirection.Input, title);
            count = dataMiner.GetOneData(command);

            return (int)CommonUtility.FilterNull(count, typeof(int));
        }

        public static int DeleteTemplates(List<int> templatesToDelete)
        {
            object count = null;
            DataMiner dataMiner = DataFactory.GetDataMiner();
            DbCommand command = DataFactory.GetCommand();
            command.CommandText = "CSP_DeleteEmailTemplates";
            dataMiner.AddParameter(command, "@Ids", DbType.String, -1, ParameterDirection.Input, DataMiner.FormatXMLForIdArray(templatesToDelete));
            dataMiner.AddParameter(command, "ReturnValue", DbType.Int32, 0, ParameterDirection.ReturnValue, null);
            dataMiner.Execute(command, out count);

            return (int)CommonUtility.FilterNull(count, typeof(int));
        }
    }
}
